package com.example.yichuangyun.mvvm.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.viewpager.widget.ViewPager;

import com.example.yichuangyun.R;
import com.example.yichuangyun.config.Constant;
import com.example.yichuangyun.databinding.ActivityCompanyDetailsBinding;
import com.example.yichuangyun.model.call.TabModel;
import com.example.yichuangyun.model.company.CompanyDataModel;
import com.example.yichuangyun.mvvm.base.BaseActivity;
import com.example.yichuangyun.mvvm.ui.fragment.CustomInfoFragment;
import com.example.yichuangyun.mvvm.ui.fragment.OfficialInfoFragment;
import com.example.yichuangyun.mvvm.ui.fragment.view.OfficialInfoViewModel;
import com.example.yichuangyun.ui.adapter.ViewPagerAdapter;
import com.example.yichuangyun.utils.ToastHelper;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;

/**
 * 企业详情
 */
public class CompanyDetailsActivity extends BaseActivity<ActivityCompanyDetailsBinding, OfficialInfoViewModel> {

    private final ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private String[] titles;
    private final ArrayList<Fragment> fragments = new ArrayList<>();
    private String reportAddress;
    private int from;

    @Override
    protected OfficialInfoViewModel bindModel() {
        return getViewModel(this, OfficialInfoViewModel.class);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    public void initData() {
        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            from = bundle.getInt(Constant.fromKey, 0);
            String eid = bundle.getString(Constant.eidKey);
            String creditNo = bundle.getString(Constant.creditNoKey);
            reportAddress = bundle.getString(Constant.reportAddressKey);
            if (TextUtils.isEmpty(eid)) {
                if (!TextUtils.isEmpty(creditNo)) {
                    showLoadingDialog();
                    viewModel.getEid(creditNo);
                } else {
                    binding.noDataLl.setVisibility(View.VISIBLE);
                }
            } else {
                showLoadingDialog();
                viewModel.getOfficialInfoDetails(eid);
            }
        }
        titles = new String[]{getString(R.string.official_info), getString(R.string.custom_info)};
        mTabEntities.add(new TabModel(titles[0]));
        mTabEntities.add(new TabModel(titles[1]));
    }

    @Override
    protected void initClick() {
        binding.ivBack.setOnClickListener(view -> finish());

        viewModel.companyModelMutableLiveData.observe(this, companyModel -> {
            if (companyModel != null && !TextUtils.isEmpty(companyModel.getEid())) {
                viewModel.getOfficialInfoDetails(companyModel.getEid());
            }
        });
        viewModel.companyDataModelMutableLiveData.observe(this, companyModel -> {
            dismissLoadingDialog();
            if (null != companyModel) {
                companyModel.setReportAddress(reportAddress);
                fragments.add(OfficialInfoFragment.newInstance(companyModel, from));
                fragments.add(CustomInfoFragment.newInstance(companyModel));
                binding.viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager(), 0, titles, fragments));
                binding.tabLayout.setTabData(mTabEntities);
            }
        });

        viewModel.mErrorData.observe(this, s -> {
            dismissLoadingDialog();
            binding.noDataLl.setVisibility(View.VISIBLE);
        });

        binding.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                binding.tabLayout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        binding.tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                binding.viewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {
            }
        });
    }
}